<?php
namespace app\index\controller;
use app\index\model\Users;
use app\index\model\Smscode;
use app\index\model\UsersInfo;
class Register extends \think\Controller{
    public function index(){
        $data = [];
        return view('register/index',$data);
    }
    public function save(){

        //接收从客户端提交过来的数据
        $request = \think\Request::instance();
        $data['r_user']    = $request->post('r_user');
        $data['r_email']   = $request->post('r_email');
        $data['r_tel']     = $request->post('r_tel');
        $data['r_code']    = $request->post('r_code');
        $data['r_captcha'] = $request->post('r_captcha');
        $data['r_pwd_one'] = $request->post('r_pwd_one');
        $data['r_pwd_two'] = $request->post('r_pwd_two');

        //定义验证规则
        $rules = [
            'r_user'     =>'require',
            'r_email'    =>'require|email',
            'r_tel'      =>'require|number|length:11',
            'r_code'     =>'require',
            'r_captcha'  =>'require|captcha',
            'r_pwd_one'  =>'require|min:6',
            'r_pwd_two'  =>'require|confirm:r_pwd_one',
        ];

        //修改错误提示信息
        $msg = [
            'r_user.require'      =>'昵称不能为空！',
            'r_email.require'     =>'邮箱不能为空！',
            'r_email.email'       =>'邮箱格式错误！',
            'r_tel.require'       =>'手机号码不能为空！',
            'r_tel.number'        =>'手机号码必须为数字！',
            'r_tel.length'        =>'手机号码为11位数字！',
            'r_code.require'      =>'验证码不能为空！',
            'r_captcha.require'   =>'图像验证码不能为空！',
            'r_captcha.captcha'   =>'图像验证码出错！',
            'r_pwd_one.require'   =>'密码不能为空！',
            'r_pwd_one.min'       =>'密码长度不能低于6个字符！',
            'r_pwd_two.require'   =>'确认密码不能为空！',
            'r_pwd_two.confirm'   =>'确认密码和密码不一致！',
        ];

        //实例化验证器对象
        $validate = new \think\Validate($rules,$msg);

        //对数据进行验证
        if(!$validate->check($data)){
            //显示错误信息
            $this->error($validate->getError());
        }

        //对电话号码唯一性的检验
        $users = new Users();
        $userdata = $users->where('phone',$data['r_tel'])->find();
        if($userdata){
            return $this->error('手机号码被占用，请更换手机号注册！');
        }

        //对手机验证码的检验
        $smscode = new Smscode();
        //定义过期时间,查询数据时过滤五分钟之前的数据
        $time = time() - 5*60;
        $result = $smscode
                ->where('phone',$data['r_tel'])
                ->where('create_time','>',$time)
                ->where('state',0)
                ->order('id desc')
                ->find();
        //有数据$result是对象，没有数据是null
        if(!$result || $result->code != $data['r_code']){
            $this->error('验证码错误！');
        }
        //验证码可以使用，就打一个标记
        $result->state = 1;
        $result->save();

        //对密码进行hash加密
        $data['r_pwd_one'] = password_hash($data['r_pwd_one'],PASSWORD_DEFAULT);
        //把电话号码和密码存入会员表中
        $users->phone = $data['r_tel'];
        $users->password = $data['r_pwd_one'];
        $is = $users->allowField(true)->save();
        if(!$is){
            return $this->error('注册失败，请重新注册！');
        }
        //取出会员表中的id
        $id = $users->id;
        //把用户的其他信息存入会员信息表中
        $users_info = new UsersInfo();
        $users_info->users_id = $id;
        $users_info->phone = $data['r_tel'];
        $users_info->nickname = $data['r_user'];
        $users_info->email = $data['r_email'];
        $is = $users_info->allowField(true)->save();
        if($is){
            $this->success('注册成功！请登录……','index/Login/index');
        }else{
            $this->error('注册失败！请重新注册！');
        }

    }

    /**
     * 服务器端对手机号唯一性的验证
     */
    public function check(){
        //获取从客户端通过Ajax传过来的值
        $request = \think\Request::instance();
        $phone = $request->post('phone');
        //对电话号码唯一性的检验
        $users = new Users();
        $userdata = $users->where('phone',$phone)->find();
        //有数据$userdata是对象，没有数据是null
        if($userdata){
            $arr['status'] = 1;
        }else{
            $arr['status'] = 0;
        }
        //当返回一个数组的时候tp5会自动将数据类型转换成json格式
        return $arr;
    }

    /*
    对服务器端传过来的验证码的验证
     */
    public function send(){
        //获取从客户端通过Ajax传过来的值
        $request = \think\Request::instance();
        $phone = $request->post('phone');
        //生成一个4位数的随机数
        $code = mt_rand(1000,9999);
        ##以下是发送短信的功能##
        $apikey = "3fa67ed39cd1e169e136ff24e2f12d1b"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
        $tpl_id = "2102020";
        //实例化extend/yunpian/Sms.php/Sms工具类
        $sms = new \yunpian\Sms();
        //调用发送短信的功能方法
        $is = $sms->send($phone,$apikey,$code,$tpl_id);
        //判断短信是否发送成功($is=0表示发送成功)
        if($is){
            return ['status'=>'error'];
        }
        $smscode = new Smscode();
        $data = [
            'phone'  => $phone,
            'code'   => $code,
        ];
        //将数据插入数据库
        $isdata = $smscode->allowField(true)->save($data);
        //数据插入成功$isdata等于1
        if($isdata){
            return ['status'=>'success'];
        }else{
            return ['status'=>'error'];
        }
    }

    /*
    对客户端提交过来的验证码进行验证
     */
    public function checkcode(){
        //获取从客户端通过Ajax传过来的值
        $request = \think\Request::instance();
        $phone = $request->post('phone');
        $code = $request->post('code');
        $smscode = new Smscode();
        //定义过期时间,查询数据时过滤五分钟之前的数据
        $time = time() - 5*60;
        //同一个电话号码注册多次，只取最后一条数据
        $result = $smscode
                ->where('phone',$phone)
                ->where('create_time','>',$time)
                ->where('state',0)
                ->order('id desc')
                ->find();
        //有数据$result是对象，没有数据是null
        if(!$result){
            $arr['status'] = 0;
            //当返回一个数组的时候tp5会自动将数据类型转换成json格式
            return $arr;
        }
        //如果手机号注册了，再验证验证码是否一致
        if($result->code == $code){
            $arr['status'] = 1;
            return $arr;
        }
        //如果前面两个if都没有判断就执行下面的代码
        $arr['status'] = 0;
        return $arr;

    }

    /*
    对图像验证码的处理
     */
    public function captcha(){
        $config = [
            'codeSet' => '23456789',
            'fontSize'    => '14',
            'length' =>'4',
        ];
        $captcha = new \think\captcha\Captcha($config);
        return $captcha->entry();
    }


}
